﻿/*
 * MIT License
 *Copyright (c) 2021 易开元(Easy-Open-Meta)

 *Permission is hereby granted, free of charge, to any person obtaining a copy
 *of this software and associated documentation files (the "Software"), to deal
 *in the Software without restriction, including without limitation the rights
 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *copies of the Software, and to permit persons to whom the Software is
 *furnished to do so, subject to the following conditions:

 *The above copyright notice and this permission notice shall be included in all
 *copies or substantial portions of the Software.

 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 *
 */
using EOM.TSHotelManagement.Common.Contract;
using EOM.TSHotelManagement.Common.Core;
using EOM.TSHotelManagement.Common.Util;
using EOM.TSHotelManagement.EntityFramework;
using SqlSugar;

namespace EOM.TSHotelManagement.Application
{
    /// <summary>
    /// 资产信息接口实现类
    /// </summary>
    public class AssetService : IAssetService
    {
        /// <summary>
        /// 资产信息
        /// </summary>
        private readonly GenericRepository<Asset> assetRepository;

        /// <summary>
        /// 部门
        /// </summary>
        private readonly GenericRepository<Department> deptRepository;

        /// <summary>
        /// 员工
        /// </summary>
        private readonly GenericRepository<Employee> employeeRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assetRepository"></param>
        /// <param name="deptRepository"></param>
        /// <param name="employeeRepository"></param>
        public AssetService(GenericRepository<Asset> assetRepository, GenericRepository<Department> deptRepository, GenericRepository<Employee> employeeRepository)
        {
            this.assetRepository = assetRepository;
            this.deptRepository = deptRepository;
            this.employeeRepository = employeeRepository;
        }

        /// <summary>
        /// 添加资产信息
        /// </summary>
        /// <param name="asset"></param>
        /// <returns></returns>
        public BaseResponse AddAssetInfo(CreateAssetInputDto asset)
        {
            try
            {
                if (assetRepository.IsAny(a => a.AssetNumber == asset.AssetNumber))
                {
                    return new BaseResponse() { Message = LocalizationHelper.GetLocalizedString("the asset number already exists.", "资产编号已存在"), Code = BusinessStatusCode.InternalServerError };
                }
                var entity = EntityMapper.Map<CreateAssetInputDto, Asset>(asset);
                var result = assetRepository.Insert(entity);
                if (!result)
                {
                    return new BaseResponse() { Message = LocalizationHelper.GetLocalizedString("insert asset failed.", "资产添加失败"), Code = BusinessStatusCode.InternalServerError };
                }
            }
            catch (Exception ex)
            {
                return new BaseResponse() { Message = LocalizationHelper.GetLocalizedString(ex.Message, ex.Message), Code = BusinessStatusCode.InternalServerError };
            }
            return new BaseResponse();
        }

        /// <summary>
        /// 查询资产信息
        /// </summary>
        /// <returns></returns>
        public ListOutputDto<ReadAssetOutputDto> SelectAssetInfoAll(ReadAssetInputDto asset)
        {
            //查询所有部门信息
            List<Department> depts = deptRepository.GetList();
            //查询所有员工信息
            List<Employee> employees = employeeRepository.GetList();

            var where = Expressionable.Create<Asset>();
            where = where.And(a => a.IsDelete == asset.IsDelete);

            int count = 0;
            List<Asset> assets = new List<Asset>();
            if (asset.Page != 0 && asset.PageSize != 0)
            {
                assets = assetRepository.AsQueryable().Where(where.ToExpression()).OrderBy(a => a.AssetNumber)
                    .ToPageList((int)asset.Page, (int)asset.PageSize, ref count);
            }
            else
            {
                assets = assetRepository.AsQueryable().Where(where.ToExpression()).OrderBy(a => a.AssetNumber).ToList();
                count = assets.Count;
            }

            var mapped = EntityMapper.MapList<Asset, ReadAssetOutputDto>(assets);

            mapped.ForEach(source =>
            {
                var dept = depts.SingleOrDefault(a => a.DepartmentNumber.Equals(source.DepartmentCode));
                source.DepartmentName = dept == null ? "" : dept.DepartmentName;
                var worker = employees.SingleOrDefault(a => a.EmployeeId.Equals(source.AcquiredByEmployeeId));
                source.AcquiredByEmployeeName = worker == null ? "" : worker.EmployeeName;
                source.AssetValueFormatted = source.AssetValue == 0 ? "" : Decimal.Parse(source.AssetValue.ToString()).ToString("#,##0.00").ToString();
            });

            return new ListOutputDto<ReadAssetOutputDto>
            {
                Data = new PagedData<ReadAssetOutputDto>
                {
                    Items = mapped,
                    TotalCount = count
                }
            };
        }

        /// <summary>
        /// 更新资产信息
        /// </summary>
        /// <param name="asset"></param>
        /// <returns></returns>
        public BaseResponse UpdAssetInfo(UpdateAssetInputDto asset)
        {
            try
            {
                if (!assetRepository.IsAny(a => a.AssetNumber == asset.AssetNumber))
                {
                    return new BaseResponse() { Message = LocalizationHelper.GetLocalizedString("asset number does not exist.", "资产编号不存在"), Code = BusinessStatusCode.InternalServerError };
                }
                var entity = EntityMapper.Map<UpdateAssetInputDto, Asset>(asset);
                var result = assetRepository.Update(entity);
                if (!result)
                {
                    return new BaseResponse() { Message = LocalizationHelper.GetLocalizedString("update asset failed.", "资产更新失败"), Code = BusinessStatusCode.InternalServerError };
                }
            }
            catch (Exception ex)
            {
                return new BaseResponse() { Message = LocalizationHelper.GetLocalizedString(ex.Message, ex.Message), Code = BusinessStatusCode.InternalServerError };
            }
            return new BaseResponse();
        }

        /// <summary>
        /// 删除资产信息
        /// </summary>
        /// <param name="asset"></param>
        /// <returns></returns>
        public BaseResponse DelAssetInfo(DeleteAssetInputDto asset)
        {
            try
            {
                if (!assetRepository.IsAny(a => a.AssetNumber == asset.AssetNumber))
                {
                    return new BaseResponse() { Message = LocalizationHelper.GetLocalizedString("asset number does not exist.", "资产编号不存在"), Code = BusinessStatusCode.InternalServerError };
                }
                assetRepository.SoftDelete(new Asset { IsDelete = asset.IsDelete, AssetNumber = asset.AssetNumber, Id = asset.Id });
            }
            catch (Exception ex)
            {
                return new BaseResponse() { Message = LocalizationHelper.GetLocalizedString(ex.Message, ex.Message), Code = BusinessStatusCode.InternalServerError };
            }
            return new BaseResponse();
        }
    }
}
